package com.lhy.tool.generator;

import com.lhy.tool.autoconfigation.GeneratorProperties;
import com.lhy.tool.autoconfigation.GeneratorProperties.Project;
import com.lhy.tool.model.ControllerAttribute;
import com.lhy.tool.model.CrudBean;
import com.lhy.tool.model.CrudColumn;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * @author wutao
 * @ClassName: AbstractViewGenerator
 * @Description: 视图生成器
 * @date 2017年12月21日 上午9:33:33
 * @Copyright: Copyright (c) 2017 wisedu
 */
@Getter
@Setter
public abstract class AbstractViewGenerator extends AbstractGenerator {

    ControllerAttribute controllerAttribute;

    protected List<CrudColumn> columns;

    @Override
    protected void writeAttribute(CrudBean crudBean, GeneratorProperties generatorProperties) {
        this.columns = crudBean.getColumns();
        this.controllerAttribute = this.controllerAttribute(crudBean);
        this.writeViewAttribute(crudBean, generatorProperties);
    }

    protected abstract void writeViewAttribute(CrudBean crudBean, GeneratorProperties generatorProperties);

    private ControllerAttribute controllerAttribute(CrudBean beanModel) {
        return ControllerAttribute.builder().controllerRequestMapping(controllerRequestMapping(beanModel))
                .viewPath(viewPath(beanModel)).build();
    }

    protected String controllerRequestMapping(CrudBean beanModel) {
        return beanModel.getModelAttributes().getJavaAttributes().getControllerRequestMapping();
    }

    protected String viewPath(CrudBean beanModel) {
        return beanModel.getModelAttributes().getViewAttributes().getViewPath();
    }

    @Override
    protected String projectPath(Project project) {
        return project.getViewProjectPath();
    }

}
